///////////////////////////////////////////////////////////////////////////////
//	FILENAME:	MailTo.m 
//	SUMMARY:	Implementation of a mailto:URL -like device
//	SUPERCLASS:	MailTo:eTImage:Object
//	INTERFACE:	None
//	PROTOCOLS:	<Annotation,HTMDSupport,ASCIISupport,LaTeXSupport,Tool,
//				InspectableTarget>
//	AUTHOR:		Rohit Khare and Tom Zavisca
//	COPYRIGHT:	(c) 1994 California Institure of Technology, eText Project
///////////////////////////////////////////////////////////////////////////////
//	IMPLEMENTATION COMMENTS
//		Does its job by holding subject & address and yelling at NXApp.
///////////////////////////////////////////////////////////////////////////////
//	HISTORY
//	10/30/94:	Modified to support <InspectableTarget>
//	07/20/94:	Converted over to eTImage/eTImageComponent. (rk)
//  07/07/94:	Converted to subclass of ImageAnnotation
//	07/03/94:   Minimal Creation
///////////////////////////////////////////////////////////////////////////////

#import "MailTo.h"
#define _MailToVERSION	10

@implementation MailTo

+ toolAwake:theApp
{
	char        buf[MAXPATHLEN];
	NXBundle	*bundle;
	id 			icon=nil;

	bundle = [NXBundle bundleForClass:[MailTo class]];
	if ([bundle getPath:buf forResource:".MailTo" ofType:"tiff"] ) {
		icon=[[NXImage alloc] initFromFile:buf];
		[icon setName:"MailToIcon"];
	} else {
		NXLogError("Image not found: MailToIcon");
	}	
	[theApp   registerAnnotation: [MailTo class] 
							name: "MailTo"
					RTFDirective: "MailTo"
					   menuLabel: "HTML/Insert mailTo:..."
						 menuKey: '\0'
						menuIcon: (NXImage *) icon];
	//[theApp registerType:NXCreateFileContentsPboardType("mailTo") for:[MailTo class]];
	return self;
}

- init
{
	[super init];
	[self setUsesButtonStyle:NO];
	[self setDraggable:YES];
	[self setImageComponent:[eTImageComponent newImageNamed:"MailToIcon"]];
	address = NXUniqueString(NXUserName());
	subject = NXUniqueString("No Subject");
	
	return self;
}

- initFromPboard:thePboard inDoc:theDoc linked:(BOOL) linked
{
	[super initFromPboard:thePboard inDoc:theDoc linked:linked];
	//[self init];
	[imageComponent setDoc:theDoc];
	subject = NXUniqueString([[etDoc docInfo] docTitle]);
	return self;
}

- readRichText:(NXStream *)stream forView:view 
{
	int i;
	char buf[MAXPATHLEN];
	
	NXScanf(stream, "%d ", &i);
	if (i != _MailToVERSION) {
		// bad version block.
	 NXLogError("MailTo found unparseable version %d at position %d",
					i, NXTell(stream));
		return nil;
	}
	NXScanf(stream, "%d", &i); NXGetc(stream); //space-eater
	if(i) NXRead(stream,buf,i);
	buf[i]=0;
	address=NXUniqueString(buf);
	NXScanf(stream, "%d", &i); NXGetc(stream); //space-eater
	if(i) NXRead(stream,buf,i);
	buf[i]=0;
	subject=NXUniqueString(buf);
	NXGetc(stream); // trailing space
	[super readRichText:stream forView:view];
	return self;
}

- writeRichText:(NXStream *)stream forView:view
{
	NXPrintf(stream, "%d %d %s %d %s ", _MailToVERSION, 
			 strlen(address), address,strlen(subject),subject);
	[super writeRichText:stream forView:view];
	return self;
}

- writeASCIIRef:(NXStream *)stream forView:view
{
	NXPrintf(stream, "Send e-mail to %s about %s\n\t",address,subject);
	[super writeASCIIRef:stream forView:view];
	return self;
}

- writeHTML:(NXStream *)stream forView: view
{
	int i;
	
	NXPrintf(stream,"<A HREF=\"mailto:%V\">",address);
	[super writeHTML:stream forView:view];
	NXPrintf(stream," to comment on <I>%v</I></A>", subject);
	return self;
}
- writeLaTeX:(NXStream *)stream forView: view
{	
	[super writeLaTeX:stream forView:view];
	NXPrintf(stream,"\n\\footnote{send e-mail to {\tt %w} about{\tt %w}}\n", address, subject);
	return self;
}

- click:(NXEvent*)e
	{ NXBeep(); return self;}
- doubleClick:(NXEvent *)e
	{ return [self sendMail:self];}
- inspect:(NXEvent*)e
	{ [[NXApp inspector] inspect:self];return self;}
- (id <Inspectable>) inspectableDelegate {
	return [[MailToUI new] setAnnotation:self]; }

- sendMail:sender
{	
	if (address && subject){
		char	bodyStr[4*MAXPATHLEN];
		id		dI;
		
		dI = [etDoc docInfo];
		sprintf(bodyStr, "---\n This message is re: the eText document %s written by %s", [dI docTitle], [dI author]);
		[NXApp 	sendMailTo: address cc:"" subject: subject 
				body: bodyStr deliver:NO];
	}
	return self;
}

- setSubject: (NXAtom) newSubject
	{subject = newSubject; return self;}
- setAddress: (NXAtom) newAddress
	{address = newAddress; return self;}
- (NXAtom) subject {return subject;}
- (NXAtom) address {return address;}
- setImageComponent:newImageComponent
{
	const char *name;
	
	name = [[newImageComponent theImage] name];
	if (name && !strcmp(name,"MailToIcon"))
		usesButtonStyle = NO;
	else
		usesButtonStyle = YES;
	return [super setImageComponent:newImageComponent];
}
@end